﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// ===============================================================================

using DevExpress.XtraLayout.Utils;
using System;
using DevExpress.XtraEditors;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    public partial class FrmUserEdit : BaseForm
    {
        public FrmUserEdit(EditFormArgs args)
        {
            InitializeComponent();
            layoutControl.Args = args;
        }

        private async void FrmRoleEdit_Load(object sender, EventArgs e)
        {
            editEntIdIayoutControlItem.Visibility = LayoutVisibility.Never;
            await layoutControl.BindingModelAsync();
        }

        private async void BtnSave_Click(object sender, EventArgs e)
        {
            await layoutControl.SaveModelAsync();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogOk();
        }

        private void EditPwdMustModify_CheckedChanged(object sender, EventArgs e)
        {
            editPwdDenyModify.Enabled = 
                editPwdNeverExpire.Enabled = !editPwdMustModify.Checked;
        }

        private void EditPwdAllowedModify_CheckedChanged(object sender, EventArgs e)
        {
            editPwdMustModify.Enabled = !editPwdDenyModify.Checked && !editPwdNeverExpire.Checked;
        }

        private void EditPwdNeverExpire_CheckedChanged(object sender, EventArgs e)
        {
            editPwdMustModify.Enabled = !editPwdDenyModify.Checked && !editPwdNeverExpire.Checked;
        }

        private void IsEntToggleSwitch_EditValueChanged(object sender, EventArgs e)
        {
            editEntIdIayoutControlItem.Visibility = IsEntToggleSwitch.EditValue.ToBool() ? LayoutVisibility.Always : LayoutVisibility.Never;
        }

        private void LayoutControl_BindingForm(object sender, EditFormPanelBindingEventArgs e)
        {
            e.Task = async () =>
            {
                if (!e.Created) //编辑
                {
                    layoutPassword.Visibility = layoutConfirmPassword.Visibility = LayoutVisibility.Never;
                    editAccount.ReadOnly = true;
                }

                //控制管理员控件
                layoutAdminVisible.Visibility =
                    layoutIsAdmin.Visibility = WinFormHelper.LayoutItemVisible(SysApp.CurrentUser.IsAdmin);
                var model = await SysUserService.Instance.GetOrCreateAsync(e.Args);
                bindingSource.DataSource = model;
            };
        }

        private void LayoutControl_SaveForm(object sender, EditFormPanelSaveEventArgs e)
        {
            e.Task = () => SysUserService.Instance.SaveAsync(e.Args.Created, (SysUser)bindingSource.DataSource);
        }

        private async void LayoutControl_ResetForm(object sender, EditFormPanelResetEventArgs e)
        {
            bindingSource.DataSource = await SysUserService.Instance.NewAsync();
            await layoutControl.BindingModelAsync();
        }

        private void EditEntId_SelectedChanged(object sender, PopupGridControlPagingSelectedEventArgs e)
        {
            if (bindingSource.DataSource is SysUser user)
            {
                user.EntName = e.Text;
            }
        }
    }
}